home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / QF2.ZIP / LOADGAME.C < prev    next >
Text File  |  1995-04-10  |  16KB  |  472 lines

  1. /**********************************************************************\
  2. *                                                                      *
  3. *  LOADGAME.C                                                          *
  4. *                                                                      *
  5. *  Copyright 1993, 1995 Diana Gruber. All Rights Reserved.             *
  6. *  Last modified: April 10, 1995                                       *
  7. *                                                                      *
  8. *  This source code is provided "as is" without any warranties, etc.   *
  9. *                                                                      *
  10. *  Load the data files, initialize the graphics environment, handle    *
  11. *  text, termination, and some other miscellaneous functions.          *
  12. *  This file contains just about all the functions that are not        *
  13. *  critical to game animation.  Those functions are all in the file    *
  14. *  QF.C. They are grouped in a single source code file so they can     *
  15. *  be declared "near" for a slight speed boost.  Functions in this     *
  16. *  file are assumed to be far if compiled in real mode using the       *
  17. *  medium model.                                                       *
  18. *                                                                      *
  19. *  For readibility, functions are stored alphabetically.               *
  20. *                                                                      *
  21. \**********************************************************************/
  22.  
  23. #define loadgame
  24. #include "defs.h"
  25.  
  26. /**********************************************************************\
  27. *                                                                      *
  28. *  clear_layout                                                        *
  29. *  Set the layout array to all zeros                                   *
  30. *                                                                      *
  31. \**********************************************************************/
  32.  
  33. void clear_layout()
  34. {
  35.    register short i,j;
  36.    for (i = 0; i < 22; i++)
  37.       for (j = 0; j < 15; j++)
  38.          layout[hidden][i][j] = FALSE;
  39. }
  40.  
  41. /**********************************************************************\
  42. *                                                                      *
  43. * fcopy_array                                                          *
  44. * copy a string from far memory to near memory                         *
  45. * (unnecessary in protected mode)                                      *
  46. *                                                                      *
  47. \**********************************************************************/
  48.  
  49. void fcopy_array(char far *s, char *t, short nchar)
  50. {
  51.    register short i;
  52.  
  53.    for (i = 0; i < nchar; i++)
  54.       *s++ = *t++;
  55. }
  56.  
  57. /**********************************************************************\
  58. *                                                                      *
  59. * getseed -- get a seed for the random number generator                *
  60. *                                                                      *
  61. \**********************************************************************/
  62.  
  63. void getseed()
  64. {
  65.    seed = (short)(fg_getclock() & 0x7FFF);
  66. }
  67.  
  68. /**********************************************************************\
  69. *                                                                      *
  70. *  init_globals                                                        *
  71. *  create player and score objects and initialize some global          *
  72. *  variables                                                           *
  73. *                                                                      *
  74. \**********************************************************************/
  75.  
  76. void init_globals()
  77. {
  78.    char *array;
  79.    SPRITE *image;
  80.  
  81.    /* create the score object */
  82.  
  83.    array = (char *)malloc(60*5+1);
  84.    score = (OBJp)malloc(sizeof(OBJ)+1);
  85.    score->action = &new_score;  /* does this work?  integer address requires "near" declaration
  86.                                    which means it should be in the same source code file. Hmmm */
  87.    image = (SPRITE *)malloc(sizeof(SPRITE)+1);
  88.    image->bitmap = array;
  89.    image->height = 5;
  90.    image->width = 60;
  91.    image->xoffset = 0;
  92.    image->yoffset = 0;
  93.    score->image = image;
  94.  
  95.    /* create the player object */
  96.    player = (OBJp)malloc(sizeof(OBJ)+1);
  97.    player->x = 26;
  98.    player->y = 164;
  99.    player->frame = 0;
  100.    player->xspeed = -8;
  101.    player->max_xspeed = 8;
  102.    player->yspeed = 0;
  103.    player->tile_xmin = 2;
  104.    player->tile_xmax = 16;
  105.    player->tile_ymin = 5;
  106.    player->attached_sprite = (OBJp)NULL;
  107.    player->tile_ymax = 10;
  108.    player->image = fighter[0];
  109.  
  110.    /* initialize some global variables */
  111.    player_timer = 0;
  112.    player_time_target = 75;
  113.    bullet_count = 0;
  114.    nhits = 0;
  115.    hit_value = 990;
  116.    player_score = 0L;
  117.    scrolled = FALSE;
  118.    frame_count = 0;
  119.    nbullets = 0;
  120.    nenemies = 0;
  121.    autopilot = FALSE;
  122.  
  123.    /* origin coordinates */
  124.    tile_orgx = 0;
  125.    tile_orgy = 0;
  126.    screen_orgx = 0;
  127.    screen_orgy = 0;
  128.    vpo = 0;
  129.    hpo = 240;
  130.    vpb = vpo+239;
  131.    hpb = hpo+239;
  132.    tpo = 480;
  133.    visual = 0;
  134.    hidden = 1;
  135.  
  136.    /* start the random number generator */
  137.    getseed();
  138. }
  139.  
  140. /**********************************************************************\
  141. *                                                                      *
  142. * init_graphics -- initialize the graphics environment                 *
  143. *                                                                      *
  144. \**********************************************************************/
  145.  
  146. void init_graphics()
  147. {
  148. #ifdef ModeX
  149.    fg_setmode(20);   /* Mode X at a 320*200 resolution */
  150.    fg_resize(352,727);
  151. #else
  152.    fg_setmode(19);   /* Mode 13h */
  153.    fg_vbinit();
  154.    if ((virtbuff1 = (char *)malloc(352*727)) == (char *)NULL)
  155.    {
  156.       sprintf(abort_string,"out of virtual buffer memory");
  157.       terminate_game();
  158.    }
  159.    workvb = fg_vbdefine(virtbuff1,352,727);
  160.    fg_vbopen(workvb);
  161. #endif
  162.  
  163.    /* uncomment this line to turn off the vertical retrace */
  164. //   fg_waitvr(0);
  165. }
  166.  
  167. /**********************************************************************\
  168. *                                                                      *
  169. *  intro_screens                                                       *
  170. *  Display the title screen and the credits screen                     *
  171. *                                                                      *
  172. \**********************************************************************/
  173.  
  174. void intro_screens()
  175. {
  176.    register short i;
  177.    char *array;
  178.    short x;
  179.  
  180.    /* display title screen, copy to visual */
  181.    fg_move(0,hpo);
  182. #ifdef ModeX
  183.    fg_showpcx("qf1.pcx",2);
  184.    fg_transfer(0,319,hpo,hpb,0,vpb,0,0);
  185. #else
  186.    fg_loadpcx("qf1.pcx",2);
  187.    fg_vbpaste(0,319,hpo,hpo+199,0,199);
  188. #endif
  189.    fg_waitfor(32);
  190. }
  191.  
  192. /**********************************************************************\
  193. *                                                                      *
  194. * irandom                                                              *
  195. * Generate a pseudo-random number between any two integers.            *
  196. *                                                                      *
  197. \**********************************************************************/
  198.  
  199. irandom(short min, short max)
  200. {
  201.    register short temp;
  202.  
  203.    temp = seed ^ (seed >> 7);
  204.    seed = ((temp << 8) ^ temp) & 0x7FFF;
  205.    return((seed % (max-min+1)) + min);
  206. }
  207.  
  208. /**********************************************************************\
  209. *                                                                      *
  210. *  load_level                                                          *
  211. *  Read the level information from a file                              *
  212. *                                                                      *
  213. \**********************************************************************/
  214.  
  215. void load_level()
  216. {
  217.    register short i,j;
  218.    char buffer[MAXROWS];
  219.  
  220.    fg_move(0,480);
  221. #ifdef ModeX
  222.    /* display the pcx file containing the tiles */
  223.    fg_showpcx("QF.PCX",2);
  224. #else
  225.    /* load the pcx file containing the tiles into a virtual buffer*/
  226.    fg_loadpcx("QF.PCX",2);
  227. #endif
  228.    /* open the level file containing the map information */
  229.  
  230.    if ((tstream = fopen("QF.LEV","rb")) == NULL)
  231.    {
  232.       sprintf(abort_string,"QF.LEV not found");
  233.       terminate_game();
  234.    }
  235.  
  236.    /* read the number of rows and columns */
  237.    fread(&ncols,sizeof(short),1,tstream);
  238.    fread(&nrows,sizeof(short),1,tstream);
  239.  
  240.    /* read all the rows */
  241.    for (i = 0; i < ncols; i++)
  242.    {
  243.       fread(buffer,sizeof(char),nrows,tstream);
  244.       fcopy_array(&backtile[i][0],buffer,nrows);
  245.    }
  246.    fclose(tstream);
  247.  
  248.    /* rows must be even -- adjust for odd number of rows */
  249.    if (ncols%2 != 0)
  250.       ncols--;
  251.  
  252.    /* calculate the maximum world coordinates */
  253.    world_maxx = ncols*16-1;
  254.    world_maxy = nrows*16-1;
  255.  
  256.    /* add another 22 columns at the end of the array for circular scroll */
  257.    for (i = 0; i < 22; i++)
  258.    {
  259.       for (j = 0; j < nrows; j++)
  260.          backtile[ncols+i][j] = backtile[i+54][j];
  261.    }
  262.    ncols+=22;
  263. }
  264.  
  265. /**********************************************************************\
  266. *                                                                      *
  267. *  load_sprite                                                         *
  268. *  Read the bitmapped sprites from a file and store them in            *
  269. *  structures.                                                         *
  270. *                                                                      *
  271. \**********************************************************************/
  272.  
  273. void load_sprite()
  274. {
  275.    register short i,n;
  276.    short nbytes;
  277.    char *bitmap;
  278.    short width,height;
  279.    SPRITE *new_sprite;
  280.  
  281.    /* open the bitmap file i you can find it */
  282.    if ((tstream = fopen("QF.BMP","rb")) == NULL)
  283.    {
  284.       sprintf(abort_string,"QF.BMP not found");
  285.       terminate_game();
  286.    }
  287.  
  288.    i = 0;
  289.  
  290.    /* how many sprites are we talking about? */
  291.    fread(&nsprites,sizeof(short),1,tstream);
  292.  
  293.    /* read all the sprites */
  294.    for (n = 0; n < nsprites; n++)
  295.    {
  296.       /* read the width and height and calculate the size of the bitmap */
  297.       fread(&width,sizeof(short),1,tstream);
  298.       fread(&height,sizeof(short),1,tstream);
  299.       nbytes = width * height;
  300.  
  301.       /* allocate space for the bitmap */
  302.       if ((bitmap = (char *)malloc(nbytes)) == (char *)NULL)
  303.       {
  304.          sprintf(abort_string,"nsprites=%d n=%d width=%d height=%d nbytes=%d\n",
  305.              nsprites,n,width, height, nbytes);
  306.          terminate_game();
  307.       }
  308.  
  309.       /* allocate space for the sprite, which is a structure including 
  310.          the pointer to the previously allocated bitmap */
  311.       if ((new_sprite = (SPRITE *)malloc(sizeof(SPRITE)+1)) == (SPRITE *)NULL)
  312.       {
  313.          sprintf(abort_string,"Out of sprite memory");
  314.          terminate_game();
  315.       }
  316.  
  317.       /* put this pointer in the sprite array */
  318.       sprite[i] = new_sprite;
  319.  
  320.       /* now read the bitmap */
  321.       fread(bitmap,sizeof(char),nbytes,tstream);
  322.  
  323.       /* initialize the sprite variables */
  324.       sprite[i]->bitmap = bitmap;
  325.       sprite[i]->width = width;
  326.       sprite[i]->height = height;
  327.       sprite[i]->xoffset = 0;
  328.       sprite[i]->yoffset = 0;
  329.       i++;
  330.    }
  331.    fclose(tstream);
  332.  
  333.    /* assign sprites to the fighter arrays and give them y offsets */
  334.    fighter[0] = sprite[0];
  335.    fighter[1] = sprite[9];
  336.    fighter[2] = sprite[1];
  337.    fighter[3] = sprite[10];
  338.    fighter[4] = sprite[3];
  339.    fighter[5] = sprite[11];
  340.    fighter[6] = sprite[2];
  341.    fighter[7] = sprite[12];
  342.  
  343.    /* also create an explosion array */
  344.    for (i = 0; i < 11; i++)
  345.       explosion[i] = sprite[i+14];
  346.  
  347.    /* center the fighter */
  348.    for (i = 0; i < 8; i++)
  349.    {
  350.       fighter[i]->yoffset = fighter[i]->height/2 - fighter[2]->height/2;
  351.    }
  352.  
  353.    /* center the explosions */
  354.    for (i = 1; i < 11; i++)
  355.    {
  356.       explosion[i]->xoffset = explosion[0]->width - explosion[i]->width/2;
  357.       explosion[i]->yoffset = explosion[i]->height/2 - explosion[0]->height;
  358.    }
  359. }
  360.  
  361. /**********************************************************************\
  362. *                                                                      *
  363. *  put_btext                                                           *
  364. *                                                                      *
  365. *  This is a simple, efficient character font.  Capital letters only.  *
  366. *  Not all the characters are included, only the ones we need.         *
  367. *  The letters are defined as 5 x 5 single color bitmaps.              *
  368. *                                                                      *
  369. \**********************************************************************/
  370.  
  371. char chars[37][5] = {
  372.    0,   0,   0,   0,   0,
  373. -120,  -8,-120,  80,  32, /* a */
  374.  -16,-120, -16,-120, -16, /* b */
  375.  120,-128,-128,-128, 120, /* c */
  376.  -16,-120,-120,-120, -16, /* c */
  377.   -8,-128, -16,-128,  -8, /* e */
  378. -128,-128, -16,-128, -16, /* f */
  379.  112,-120,-104,-128, 120, /* g */
  380. -120,-120,  -8,-120,-120, /* h */
  381.  112,  32,  32,  32, 112, /* i */
  382.   96,-112,  16,  16,  56, /* j */
  383. -120,-112, -32,-112,-120, /* k */
  384.   -8,-128,-128,-128,-128, /* l */
  385. -120, -88, -88, -40,-120, /* m */
  386. -120,-104, -88, -56,-120, /* n */
  387.  112,-120,-120,-120, 112, /* o */
  388. -128,-128, -16,-120, -16, /* p */
  389.  120, -88,-120,-120, 112, /* q */
  390. -112, -96, -16,-120, -16, /* r */
  391.  -16,   8, 112,-128, 120, /* s */
  392.   32,  32,  32,  32,  -8, /* t */
  393.  112,-120,-120,-120,-120, /* u */
  394.   32,  80,-120,-120,-120, /* v */
  395. -120, -40, -88, -88,-120, /* w */
  396. -120,  80,  32,  80,-120, /* x */
  397.   32,  32,  32,  80,-120, /* y */
  398.   -8,  64,  32,  16,  -8, /* x */
  399.  112,-120,-120,-120, 112, /* 0 */
  400.  112,  32,  32,  96,  32, /* 1 */
  401.  -16,  64,  32,-112,  96, /* 2 */
  402.  -32,  16,  96,  16, -32, /* 3 */
  403.   16,  16, -16,-112,-112, /* 4 */
  404.  112,   8, -16,-128,  -8, /* 5 */
  405.  112,-120, -16,-128, 112, /* 6 */
  406.   64,  64,  32,  16,  -8, /* 7 */
  407.  112,-120, 112,-120, 112, /* 8 */
  408.   16,   8, 120,-120, 112, /* 9 */
  409. };                                        
  410.  
  411. put_bstring(char *string,short nchar,short ix,short iy)
  412. {
  413.    register short i;
  414.    short blank;
  415.    char ch;
  416.    
  417.    for (i = 0; i < nchar; i++)
  418.    {
  419.       blank = FALSE;
  420.       ch = string[i];
  421.  
  422.       /* upper case */
  423.       if (ch >= 65 && ch <= 90) 
  424.          ch -= 64;
  425.  
  426.       /* lower case */
  427.       else if (ch >= 97 && ch <= 122)
  428.          ch -= 96;   
  429.  
  430.       /* numbers */
  431.       else if (ch >= 48 && ch <= 57) 
  432.          ch -= 21;
  433.  
  434.       else 
  435.          blank = TRUE;
  436.  
  437.       fg_move(ix,iy);
  438.       if (!blank) fg_drawmap(&chars[ch][0],1,5);
  439.       ix += 6;
  440.    }
  441.    return(0);
  442. }
  443.  
  444. /**********************************************************************\
  445. *                                                                      *
  446. *  terminate_game                                                      *
  447. *  If for some reason we can't continue, like for example if a data    *
  448. *  file is missing, do an abnormal termination.                        *
  449. *                                                                      *
  450. \**********************************************************************/
  451.  
  452. void terminate_game()
  453. {
  454.    /* disable the keyboard handler */
  455.    fg_kbinit(0);
  456.  
  457.    /* close the virtual buffers */
  458.    fg_vbclose();
  459.  
  460.    /* set the video mode to text mode */
  461.    fg_setmode(3);
  462.  
  463.    /* reset the screen attributes (if ANSI.SYS is loaded ) */
  464.    fg_reset();
  465.  
  466.    /* print the exit screen and exit */
  467.    printf(abort_string);
  468.    printf("\n");
  469.    exit(0);
  470. }
  471.  
  472.